﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.Domain.Products;
using MangoMes.Commont.Domain.Purchase;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MathNet.Numerics.Distributions;
using MediatR;

namespace MangoMes.Write.API.Applications.PurchaseStorags
{
    public class PurchaseStorageAddHandler : IRequestHandler<PurchaseStorageAddCommand, ResultsDTO>
    {
        private readonly IBateRepository<PurchaseStorage> purchase;
        private readonly ILogger<PurchaseStorageAddHandler> logger;
        public PurchaseStorageAddHandler(IBateRepository<PurchaseStorage> purchase, ILogger<PurchaseStorageAddHandler> logger)
        {
            this.purchase = purchase;
            this.logger = logger;
        }
        /// <summary>
        /// 采购入库添加
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultsDTO> Handle(PurchaseStorageAddCommand request, CancellationToken cancellationToken)
        {
            try
            {
                DapperHelper helper = new DapperHelper();
                ResultsDTO dto = new ResultsDTO() { Code = 0 };
                PurchaseStorage purch = new PurchaseStorage();
                purch.PurchaseNo = request.PurchaseNo;
                purch.PurchaseName = request.PurchaseName;
                purch.PurchaseDate = request.PurchaseDate;
                purch.ArrivalNoticeId = request.ArrivalNoticeId;
                purch.PurchaseIndentId = request.PurchaseIndentId;
                purch.SupplierId = request.SupplierId;
                purch.ReceiptState = "草稿";
                purch.Remark = request.Remark;
                purch.IsNo = 0;
                string conn = helper.MangoMesWrite;
                string sql = "insert into purchasestorage values(@PurchaseId,@PurchaseNo,@PurchaseName,@PurchaseDate,@ArrivalNoticeId,@PurchaseIndentId,@SupplierId,@ReceiptState,@Remark,@IsNo)";
                dto.Code = await purchase.Add(sql, purch, conn);
                logger.LogInformation("成功");
                return dto;
            }
            catch (Exception ex)
            {
                logger.LogError("失败" + ex);
                throw;
            }


          
        }
    }
}
